package com.spzx.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.utils.StringUtils;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.common.security.annotation.RequiresRoles;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.Brand;
import com.spzx.product.service.IBrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 分类品牌 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2025-03-12
 */
@Tag(name = "品牌管理")
@RestController
@RequestMapping("/brand")
public class BrandController extends BaseController {


    @Autowired
    private IBrandService brandService;

    /**
     * 获取品牌列表
     */
    // 有该权限字符串才能调用该接口
    @RequiresPermissions("product:brand:list")
    @Operation(summary = "获取品牌列表")
    @GetMapping("/list")
    public TableDataInfo list(@Parameter(name = "name", description = "品牌名称") String name) {
        //分页   // 分页查询 封装可分页插件可以自动根据查询到的列表进行分页
        startPage();
        // 判断非空 防止sql语句出错
        List<Brand> list = brandService.selectBrandList(name);
        return getDataTable(list);
    }

    /**
     * 根据id获取品牌详情
     */
    @RequiresPermissions("product:brand:query")
    @Log(title = "品牌管理", businessType = BusinessType.QUERY)
    @Operation(summary = "根据id获取品牌详情")
    @GetMapping(value = "/{brandId}")
    public AjaxResult getInfo(@Parameter(name = "brandId", description = "品牌id") @PathVariable Long brandId) {
        return success(brandService.getById(brandId));
    }


    @RequiresPermissions("product:brand:add")
    @Log(title = "品牌管理", businessType = BusinessType.INSERT)// 添加日志注解
    @Operation(summary = "新增品牌")
    @PostMapping
    public AjaxResult add(@Parameter(name = "brand", description = "品牌信息") @Validated @RequestBody Brand brand) {


        // 名称已经存在 报错
        // 根据 name查询 当前数据库表中brand是否存在
        Brand brandDB = brandService.getOne(new LambdaQueryWrapper<Brand>().eq(Brand::getName, brand.getName()));
        if (!(brandDB == null)) {
            return AjaxResult.error("品牌名称已经存在");
        }
        brand.setCreateBy(SecurityUtils.getUsername());
        return toAjax(brandService.save(brand));
    }

    //    /**
//     * 修改品牌信息
//     */
    @RequiresPermissions("product:brand:edit")
    @Log(title = "品牌管理", businessType = BusinessType.UPDATE)
    @Operation(summary = "修改品牌信息")
    @PutMapping
    public AjaxResult edit(@Parameter(name = "brand", description = "品牌信息") @Validated @RequestBody Brand brand) {

        Brand brandDB = brandService.getOne(new LambdaQueryWrapper<Brand>().eq(Brand::getName, brand.getName()).ne(Brand::getId, brand.getId()));
        if (!(brandDB == null)) {
            return AjaxResult.error("品牌名称已经存在");
        }
        brand.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(brandService.updateById(brand));
    }

    /**
     * 删除品牌
     */
    @RequiresPermissions("product:brand:remove")
    @Log(title = "品牌管理", businessType = BusinessType.DELETE)
    @Operation(summary = "删除品牌")
    @DeleteMapping("/{brandIds}")
    public AjaxResult remove(@Parameter(name = "brandId", description = "品牌id列表") @PathVariable List<Integer> brandIds) {
        return toAjax(brandService.removeBatchByIds(brandIds));
    }

}
